﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YJKArch.Utils;
using RegionSplittor.Data;
using TopologySearch;

namespace RegionSplittor.Utils
{
	public class RegionBeamCellSplitor
	{
		public static List<BeamRegionData> SplitRegion(UIDocument uiDoc, List<CurveLoop> arLoop, bool bIsFire)
		{
			List<BeamRegionData> list = new List<BeamRegionData>();
			Document document = uiDoc.Document;
			string text = string.Empty;
			Document document2 = null;
			RegionBeamCellSplitor.FloorHeightS = document.ActiveView.GenLevel.Elevation;
			RegionBeamCellSplitor.uiDocAll = uiDoc;
			try
			{
				new List<Element>();
				string linkDocPathByStr = LinkDocumentOperator.GetLinkDocPathByStr(document, "S");
				text = ProjectDocumentOperator.CopyDocument(linkDocPathByStr);
				document2 = ProjectDocumentOperator.OpenDocument(text, uiDoc.Application);
				string text2 = ProjectDocumentOperator.CopyDocument(LinkDocumentOperator.GetLinkDocPathByStr(document, "A"));
				Document document3 = ProjectDocumentOperator.OpenDocument(text2, uiDoc.Application);
				if (string.IsNullOrEmpty(linkDocPathByStr) || document2 == null || arLoop == null)
				{
					return list;
				}
				GifFormOperator.Start("");
				double viewProjectElevation = ViewOperator.GetViewProjectElevation(document.ActiveView);
				double num = RegionBeamCellSplitor.GetLevelToUpLevelHeight(document2, viewProjectElevation);
				num += RegionBeamCellSplitor.m_dUpSerch;
				List<Element> loopsElement = ElementSelector.GetLoopsElement(document2, arLoop, XYZ.BasisZ, num);
				list = RegionBeamCellSplitor.GetAllBeamRegions(document2, loopsElement, viewProjectElevation, bIsFire);
				List<Element> loopsElement2 = ElementSelector.GetLoopsElement(document3, arLoop, XYZ.BasisZ, num);
				List<BeamRegionData> allBeamRegions = RegionBeamCellSplitor.GetAllBeamRegions(document3, loopsElement2, viewProjectElevation, bIsFire);
				list.AddRange(allBeamRegions);
				ProjectDocumentOperator.CloseDocument(document2);
				ProjectDocumentOperator.DeleteDocument(text);
				ProjectDocumentOperator.CloseDocument(document3);
				ProjectDocumentOperator.DeleteDocument(text2);
				GifFormOperator.end();
			}
			catch (Exception)
			{
				if (document2 != null)
				{
					ProjectDocumentOperator.CloseDocument(document2);
					ProjectDocumentOperator.DeleteDocument(text);
				}
				GifFormOperator.end();
			}
			return list;
		}

		public static double GetLevelToUpLevelHeight(Autodesk.Revit.DB.Document doc, double dElevation)
		{
			double result = 0.0;
			Level upLevelByElevation = ElementSelector.GetUpLevelByElevation(doc, dElevation);
			if (upLevelByElevation != null)
			{
				result = upLevelByElevation.Elevation - dElevation;
			}
			return result;
		}

		private static List<Room> GetRooms(Autodesk.Revit.DB.Document doc, List<Element> arBeam, Level level, ref List<KeyValuePair<ElementId, ElementId>> WallToBeamId)
		{
			List<Room> list = new List<Room>();
			if (level == null)
			{
				return list;
			}
			WallToBeamId = WallCreator.CreateCurveWalls(doc, arBeam, level);
			Transaction transaction = new Transaction(doc);
			transaction.Start("Topology");
			PlanTopology planTopology = doc.get_PlanTopology(level);
			transaction.Commit();
			foreach (object obj in planTopology.Circuits)
			{
				PlanCircuit planCircuit = (PlanCircuit)obj;
				Transaction transaction2 = new Transaction(doc);
				try
				{
					transaction2.Start("primary");
					Room item = doc.Create.NewRoom(null, planCircuit);
					list.Add(item);
					transaction2.Commit();
				}
				catch (Exception)
				{
					transaction2.RollBack();
				}
			}
			return list;
		}

		private static List<BeamCellData> GetBeamCellsByTopology(Autodesk.Revit.DB.Document doc, List<Element> arPriSecBeam, List<ElementId> arWallId, Level level)
		{
			List<BeamCellData> list = new List<BeamCellData>();
			List<KeyValuePair<ElementId, ElementId>> wallToBeamId = null;
			List<Room> rooms = RegionBeamCellSplitor.GetRooms(doc, arPriSecBeam, level, ref wallToBeamId);
			List<List<Curve>> list2 = new List<List<Curve>>();
			foreach (Room room in rooms)
			{
				List<Curve> spaceOrRoomBoundaryCurve = YJKRevitTools.GetSpaceOrRoomBoundaryCurve(room);
				list2.Add(spaceOrRoomBoundaryCurve);
				try
				{
					BeamCellData beamCell = RegionBeamCellSplitor.GetBeamCell(room, wallToBeamId, arWallId);
					if (beamCell != null)
					{
						list.Add(beamCell);
					}
				}
				catch
				{
				}
			}
			return list;
		}

		private static List<BeamCellData> GetBeamCellsByLoopSearch(Autodesk.Revit.DB.Document doc, List<Element> arPriSecBeam, double dFloorHeight)
		{
			List<BeamCellData> list = new List<BeamCellData>();
			List<Pole2D> oneGroupPole2D = RegionBeamCellSplitor.GetOneGroupPole2D(arPriSecBeam);
			StructureLoopSearcher structureLoopSearcher = new StructureLoopSearcher(doc);
			List<List<ElementId>> list2 = new List<List<ElementId>>();
            List<List<Curve>> cellsEdgeLoops = structureLoopSearcher.GetCellsEdgeLoops(oneGroupPole2D, ref list2, StructureLoopSearcher.SearchEdgeLocation.SEL_Inside);
			if (list2.Count != cellsEdgeLoops.Count)
			{
				return list;
			}
			int num = 0;
			foreach (List<Curve> arCellCurve in cellsEdgeLoops)
			{
				List<ElementId> arCellEleId = list2[num++];
				BeamCellData beamCellByPole2D = RegionBeamCellSplitor.GetBeamCellByPole2D(doc, arCellCurve, arCellEleId, dFloorHeight);
				if (beamCellByPole2D != null)
				{
					list.Add(beamCellByPole2D);
				}
			}
			return list;
		}

		private static List<BeamRegionData> CreateRegions(Autodesk.Revit.DB.Document doc, List<BeamCellData> arPrimaryBeamCell, Level level, bool bIsFire)
		{
			List<BeamRegionData> result = new List<BeamRegionData>();
			if (arPrimaryBeamCell.Count < 1)
			{
				return result;
			}
			if (bIsFire)
			{
				result = RegionBeamCellSplitor.GetFireRegions(doc, arPrimaryBeamCell);
			}
			else
			{
				result = RegionBeamCellSplitor.GetLightRegions(doc, arPrimaryBeamCell);
			}
			return result;
		}

		public static List<BeamRegionData> GetAllBeamRegions(Autodesk.Revit.DB.Document doc, List<Element> arEle, double dElevation = 0.0, bool bIsFire = true)
		{
			List<BeamRegionData> result = new List<BeamRegionData>();
			Level levelByElevation = ElementSelector.GetLevelByElevation(doc, dElevation);
			if (levelByElevation == null)
			{
				return result;
			}
			List<ElementId> allNoConnectWalls = WallCreator.GetAllNoConnectWalls(arEle, doc);
			List<Element> allPrimaryBeam = RegionBeamCellSplitor.GetAllPrimaryBeam(arEle);
			List<BeamCellData> primaryCell = RegionBeamCellSplitor.GetPrimaryCell(doc, allPrimaryBeam, allNoConnectWalls, levelByElevation);
			return RegionBeamCellSplitor.CreateRegions(doc, primaryCell, levelByElevation, bIsFire);
		}

		public static List<BeamRegionData> GetAllBeamRegions1(ProgressBarForm progressBar, Autodesk.Revit.DB.Document doc, List<Element> arEle, double dElevation = 0.0, bool bIsFire = true)
		{
			List<BeamRegionData> result = new List<BeamRegionData>();
			Level levelByElevation = ElementSelector.GetLevelByElevation(doc, dElevation);
			if (levelByElevation == null)
			{
				return result;
			}
			progressBar.Text = "正在获取所有的墙...";
			progressBar.RefreshProgressBar();
			List<ElementId> allNoConnectWalls = WallCreator.GetAllNoConnectWalls(arEle, doc);
			progressBar.Text = "正在提取所有的主梁...";
			progressBar.RefreshProgressBar();
			List<Element> allPrimaryBeam = RegionBeamCellSplitor.GetAllPrimaryBeam(arEle);
			progressBar.Text = "正在获取所有的主区域梁格...";
			progressBar.RefreshProgressBar();
			List<BeamCellData> primaryCell = RegionBeamCellSplitor.GetPrimaryCell(doc, allPrimaryBeam, allNoConnectWalls, levelByElevation);
			progressBar.Text = "正在生成所有的区域...";
			progressBar.RefreshProgressBar();
			return RegionBeamCellSplitor.CreateRegions(doc, primaryCell, levelByElevation, bIsFire);
		}

		public static bool IsParallelWithTwoBeamId(ElementId firstId, ElementId secondId, Autodesk.Revit.DB.Document doc)
		{
			bool result = false;
			try
			{
				FamilyInstance familyInstance = doc.GetElement(firstId) as FamilyInstance;
				FamilyInstance familyInstance2 = doc.GetElement(secondId) as FamilyInstance;
				if (familyInstance != null && familyInstance2 != null)
				{
					Curve curve = (familyInstance.Location as LocationCurve).Curve;
					Curve curve2 = (familyInstance2.Location as LocationCurve).Curve;
					if (null != curve && null != curve2)
					{
						IntersectionResultArray intersectionResultArray = null;
						curve.Intersect(curve2, out intersectionResultArray);
						if (intersectionResultArray == null || intersectionResultArray.IsEmpty || intersectionResultArray.Size == 0)
						{
							XYZ xyz = curve.GetEndPoint(0) - curve.GetEndPoint(1);
							XYZ xyz2 = curve2.GetEndPoint(0) - curve2.GetEndPoint(1);
							double num = xyz.AngleTo(xyz2);
							if (Math.Abs(num - 0.0) < RegionBeamCellSplitor.m_dZeroTol || Math.Abs(num - Math.PI) < RegionBeamCellSplitor.m_dZeroTol)
							{
								result = true;
							}
						}
					}
				}
			}
			catch
			{
			}
			return result;
		}

		private static bool IsValidBeamCellSegment(List<KeyValuePair<Curve, Element>> curveToElement, List<KeyValuePair<ElementId, ElementId>> WallToBeamId, List<ElementId> arWallId)
		{
			bool result = false;
			List<ElementId> list = new List<ElementId>();
			foreach (KeyValuePair<Curve, Element> keyValuePair in curveToElement)
			{
				if (keyValuePair.Value != null && keyValuePair.Value is Wall)
				{
					list.Add(keyValuePair.Value.Id);
				}
			}
			if (list.Count < 3)
			{
				return result;
			}
			foreach (ElementId elementId in list)
			{
				bool flag = false;
				foreach (KeyValuePair<ElementId, ElementId> keyValuePair2 in WallToBeamId)
				{
					if (elementId.IntegerValue == keyValuePair2.Key.IntegerValue)
					{
						flag = true;
						break;
					}
				}
				if (!flag && arWallId.Contains(elementId))
				{
					flag = true;
				}
				if (!flag)
				{
					return result;
				}
			}
			return true;
		}

		private static List<CurveElementData> GetBeamCellCurves(Autodesk.Revit.DB.Document doc, List<KeyValuePair<Curve, Element>> curveToElement, List<KeyValuePair<ElementId, ElementId>> WallToBeamId, List<ElementId> arWallId)
		{
			List<CurveElementData> list = new List<CurveElementData>();
			foreach (KeyValuePair<Curve, Element> keyValuePair in curveToElement)
			{
				ElementId elementId = ElementId.InvalidElementId;
				if (keyValuePair.Value != null)
				{
					elementId = keyValuePair.Value.Id;
				}
				BeamOrderType bType = BeamOrderType.BT_NotBeam;
				if (keyValuePair.Value != null && keyValuePair.Value is Wall)
				{
					bool flag = false;
					foreach (KeyValuePair<ElementId, ElementId> keyValuePair2 in WallToBeamId)
					{
						if (keyValuePair2.Key.IntegerValue == keyValuePair.Value.Id.IntegerValue)
						{
							elementId = keyValuePair2.Value;
							FamilyInstance familyInstance = doc.GetElement(elementId) as FamilyInstance;
							if (familyInstance != null)
							{
								bType = BeamOperator.GetBeamOrderType(familyInstance);
							}
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						elementId = keyValuePair.Value.Id;
						bType = BeamOrderType.BT_PrimaryWall;
					}
				}
				list.Add(new CurveElementData(keyValuePair.Key, elementId, bType));
			}
			return list;
		}

		private static BeamCellData GetBeamCell(Room room, List<KeyValuePair<ElementId, ElementId>> WallToBeamId, List<ElementId> arWallId)
		{
			LocationPoint locationPoint = room.Location as LocationPoint;
			if (WallToBeamId == null || locationPoint == null)
			{
				return null;
			}
			List<KeyValuePair<Curve, Element>> curveElement = RegionBeamCellSplitor.GetCurveElement(room);
			if (!RegionBeamCellSplitor.IsValidBeamCellSegment(curveElement, WallToBeamId, arWallId))
			{
				return null;
			}
			List<CurveElementData> beamCellCurves = RegionBeamCellSplitor.GetBeamCellCurves(room.Document, curveElement, WallToBeamId, arWallId);
			BeamCellData beamCellData = new BeamCellData();
			beamCellData.m_CellArea = room.Area;
			beamCellData.m_ptCenter = locationPoint.Point;
			beamCellData.m_arCurve.AddRange(beamCellCurves);
			CurveLoop floorCurveLoop = beamCellData.GetFloorCurveLoop(room.Document);
			beamCellData.m_dFloorHeight = SmokeDetectorHeight.GetSetHeight(RegionBeamCellSplitor.uiDocAll.Document, floorCurveLoop, room.Document, RegionBeamCellSplitor.FloorHeightS);
			return beamCellData;
		}

		private static List<BeamCellData> GetPrimaryCell(Autodesk.Revit.DB.Document doc, List<Element> arPrimaryBeam, List<ElementId> arWallId, Level level)
		{
			List<BeamCellData> result = new List<BeamCellData>();
			try
			{
				result = RegionBeamCellSplitor.GetBeamCellsByTopology(doc, arPrimaryBeam, arWallId, level);
			}
			catch
			{
			}
			return result;
		}

		private static List<KeyValuePair<Curve, Element>> GetCurveElement(Room room)
		{
			List<KeyValuePair<Curve, Element>> list = new List<KeyValuePair<Curve, Element>>();
			IList<IList<BoundarySegment>> boundarySegments = room.GetBoundarySegments(new SpatialElementBoundaryOptions
			{
				SpatialElementBoundaryLocation = 0
			});
			List<BoundarySegment> list2 = null;
			if (boundarySegments.Count > 0)
			{
				list2 = boundarySegments.First<IList<BoundarySegment>>().ToList<BoundarySegment>();
			}
			if (list2 != null && list2.Count > 0)
			{
				foreach (BoundarySegment boundarySegment in list2)
				{
					if (boundarySegment != null)
					{
						list.Add(new KeyValuePair<Curve, Element>(UnifiedModified.GetBoundarySegmentCurve(boundarySegment), UnifiedModified.GetBoundarySegmentElement(room.Document, boundarySegment)));
					}
				}
			}
			return list;
		}

		private static List<Element> GetAllPrimaryBeam(List<Element> arEle)
		{
			List<Element> list = new List<Element>();
			foreach (Element element in arEle)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance != null && BeamOperator.IsPrimaryBeam(familyInstance))
				{
					list.Add(familyInstance);
				}
			}
			return list;
		}

		private static RegionType GetRegionType(BeamRegionData region, Autodesk.Revit.DB.Document doc)
		{
			if (region.GetBeamIdsByType(BeamOrderType.BT_PrimaryBeam).Count != 4)
			{
				return RegionType.RT_NotRule;
			}
			RegionType result = RegionType.RT_Rule;
			if (RegionBeamCellSplitor.IsPriSecBeamParallel(region, doc))
			{
				BeamCellData maxBeamCell = region.GetMaxBeamCell();
				BeamCellData minBeamCell = region.GetMinBeamCell();
				if (maxBeamCell != null && minBeamCell != null)
				{
					double num = maxBeamCell.m_CellArea - minBeamCell.m_CellArea;
					double num2 = minBeamCell.m_CellArea * 0.4;
					if (num > num2)
					{
						result = RegionType.RT_NotRule;
					}
				}
				else
				{
					result = RegionType.RT_NotRule;
				}
			}
			else
			{
				result = RegionType.RT_NotRule;
			}
			return result;
		}

		private static List<CurveElementData> GetRegionPrimaryCurveDatas(BeamRegionData region, Autodesk.Revit.DB.Document doc)
		{
			List<CurveElementData> list = new List<CurveElementData>();
			foreach (ElementId elementId in region.GetBeamIdsByType(BeamOrderType.BT_PrimaryBeam))
			{
				Curve beamLocCurve = BeamOperator.GetBeamLocCurve(elementId, doc);
				if (null != beamLocCurve)
				{
					list.Add(new CurveElementData(beamLocCurve, elementId, BeamOrderType.BT_PrimaryBeam));
				}
			}
			return list;
		}

		private static bool IsRegionHasSecBeam(BeamRegionData region)
		{
			bool result = false;
			using (List<BeamCellData>.Enumerator enumerator = region.m_arBeamCell.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.GetBeamIdsByType(BeamOrderType.BT_SecondaryBeam).Count > 0)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private static bool IsPriSecBeamParallel(BeamRegionData region, Autodesk.Revit.DB.Document doc)
		{
			bool result = false;
			List<ElementId> beamIdsByType = region.GetBeamIdsByType(BeamOrderType.BT_PrimaryBeam);
			List<ElementId> beamIdsByType2 = region.GetBeamIdsByType(BeamOrderType.BT_SecondaryBeam);
			foreach (ElementId elementId in beamIdsByType)
			{
				bool flag = false;
				foreach (ElementId elementId2 in beamIdsByType)
				{
					if (elementId.IntegerValue != elementId2.IntegerValue && RegionBeamCellSplitor.IsParallelWithTwoBeamId(elementId, elementId2, doc))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					return result;
				}
			}
			foreach (ElementId firstId in beamIdsByType2)
			{
				bool flag2 = false;
				foreach (ElementId secondId in beamIdsByType)
				{
					if (RegionBeamCellSplitor.IsParallelWithTwoBeamId(firstId, secondId, doc))
					{
						flag2 = true;
						break;
					}
				}
				if (!flag2)
				{
					return result;
				}
			}
			result = true;
			return result;
		}

		private static List<Element> GetBeamCellBeamWall(BeamCellData cellData, Autodesk.Revit.DB.Document doc)
		{
			List<Element> list = new List<Element>();
			if (cellData != null)
			{
				CurveLoop secondaryCurveLoop = cellData.GetSecondaryCurveLoop(doc);
				List<CurveLoop> list2 = new List<CurveLoop>();
				list2.Add(secondaryCurveLoop);
				double num = Common.MMtoIntch(500.0);
				Solid solid = null;
				try
				{
					solid = GeometryCreationUtilities.CreateExtrusionGeometry(list2, -XYZ.BasisZ, num);
				}
				catch (Exception)
				{
				}
				ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
				ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));
				ElementIntersectsSolidFilter elementIntersectsSolidFilter = new ElementIntersectsSolidFilter(solid);
				LogicalOrFilter logicalOrFilter = new LogicalOrFilter(elementCategoryFilter, elementClassFilter);
				LogicalAndFilter logicalAndFilter = new LogicalAndFilter(logicalOrFilter, elementIntersectsSolidFilter);
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				filteredElementCollector.OfClass(typeof(FamilyInstance));
				filteredElementCollector.WhereElementIsNotElementType();
				List<Element> list3 = filteredElementCollector.WherePasses(logicalAndFilter).ToElements().ToList<Element>();
				solid.Dispose();
				filteredElementCollector.Dispose();
				foreach (Element element in list3)
				{
					if (element is FamilyInstance || element is Wall)
					{
						list.Add(element);
					}
				}
				List<ElementId> beamIdsByType = cellData.GetBeamIdsByType(BeamOrderType.BT_PrimaryBeam);
				List<ElementId> beamIdsByType2 = cellData.GetBeamIdsByType(BeamOrderType.BT_PrimaryWall);
				if (beamIdsByType2.Count > 0)
				{
					beamIdsByType.AddRange(beamIdsByType2);
				}
				foreach (ElementId elementId in beamIdsByType)
				{
					Element element2 = doc.GetElement(elementId);
					if (element2 != null)
					{
						list.Add(element2);
					}
				}
				FilterMemeryManager.ReleaseFilers(new List<ElementFilter>
				{
					elementCategoryFilter,
					elementClassFilter,
					elementIntersectsSolidFilter,
					logicalOrFilter,
					logicalAndFilter
				});
			}
			return list;
		}

		private static List<BeamRegionData> GetLightRegions(Autodesk.Revit.DB.Document doc, List<BeamCellData> arPrimaryBeamCell)
		{
			List<BeamRegionData> list = new List<BeamRegionData>();
			foreach (BeamCellData item in arPrimaryBeamCell)
			{
				BeamRegionData beamRegionData = new BeamRegionData();
				beamRegionData.m_regionDoc = doc;
				beamRegionData.m_nCellCount = 1;
				beamRegionData.m_arBeamCell.Add(item);
				beamRegionData.m_bHasSecondary = false;
				beamRegionData.m_regionType = RegionType.RT_NotRule;
				beamRegionData.m_arPrimaryCurveData = RegionBeamCellSplitor.GetRegionPrimaryCurveDatas(beamRegionData, doc);
				list.Add(beamRegionData);
			}
			return list;
		}

		private static List<BeamRegionData> GetFireRegions(Autodesk.Revit.DB.Document doc, List<BeamCellData> arPrimaryBeamCell)
		{
			List<BeamRegionData> list = new List<BeamRegionData>();
			foreach (BeamCellData beamCellData in arPrimaryBeamCell)
			{
				List<Element> beamCellBeamWall = RegionBeamCellSplitor.GetBeamCellBeamWall(beamCellData, doc);
				if (beamCellBeamWall.Count >= 3)
				{
					bool flag = false;
					foreach (Element element in beamCellBeamWall)
					{
						if (element is FamilyInstance)
						{
							flag = BeamOperator.IsSecondaryBeam(element as FamilyInstance);
							if (flag)
							{
								break;
							}
						}
					}
					if (!flag)
					{
						BeamRegionData beamRegionData = new BeamRegionData();
						beamRegionData.m_regionDoc = doc;
						beamRegionData.m_nCellCount = 1;
						beamRegionData.m_arBeamCell.Add(beamCellData);
						beamRegionData.m_bHasSecondary = false;
						beamRegionData.m_regionType = RegionType.RT_NotRule;
						beamRegionData.m_arPrimaryCurveData = RegionBeamCellSplitor.GetRegionPrimaryCurveDatas(beamRegionData, doc);
						list.Add(beamRegionData);
					}
					else
					{
						List<BeamCellData> beamCellsByLoopSearch = RegionBeamCellSplitor.GetBeamCellsByLoopSearch(doc, beamCellBeamWall, beamCellData.m_dFloorHeight);
						if (beamCellsByLoopSearch.Count > 0)
						{
							BeamRegionData beamRegionData2 = new BeamRegionData();
							beamRegionData2.m_regionDoc = doc;
							beamRegionData2.m_nCellCount = beamCellsByLoopSearch.Count;
							beamRegionData2.m_arBeamCell.AddRange(beamCellsByLoopSearch);
							beamRegionData2.m_bHasSecondary = RegionBeamCellSplitor.IsRegionHasSecBeam(beamRegionData2);
							beamRegionData2.m_regionType = RegionBeamCellSplitor.GetRegionType(beamRegionData2, doc);
							beamRegionData2.m_arPrimaryCurveData = RegionBeamCellSplitor.GetRegionPrimaryCurveDatas(beamRegionData2, doc);
							list.Add(beamRegionData2);
						}
					}
				}
			}
			return list;
		}

		private static List<Pole2D> GetOneGroupPole2D(List<Element> arEle)
		{
			List<Pole2D> list = new List<Pole2D>();
			foreach (Element element in arEle)
			{
				if (BeamOperator.IsBeamOrWall(element))
				{
					Curve curve = null;
					double num = 0.0;
					LocationCurve locationCurve = element.Location as LocationCurve;
					if (locationCurve != null)
					{
						curve = locationCurve.Curve;
					}
					if (!(null == curve))
					{
						if (element is Wall)
						{
							num = (element as Wall).Width;
						}
						else if (element is FamilyInstance)
						{
							FamilyInstance familyInstance = element as FamilyInstance;
							num = BeamOperator.GetEleParamDoubleValue(element.Document.GetElement(familyInstance.GetTypeId()), "b");
						}
						XYZ xyz = curve.GetEndPoint(1) - curve.GetEndPoint(0);
						XYZ ptStart = curve.GetEndPoint(0) - xyz.Normalize() * NumbericTools.mmToFeet(500.0);
						XYZ ptEnd = curve.GetEndPoint(1) + xyz.Normalize() * NumbericTools.mmToFeet(500.0);
						double num2 = num / 2.0;
						Pole2D item = Pole2D.NewStructure2DPole(element.Id, ptStart, ptEnd, num2, num2);
						list.Add(item);
					}
				}
			}
			return list;
		}

		private static BeamCellData GetBeamCellByPole2D(Autodesk.Revit.DB.Document doc, List<Curve> arCellCurve, List<ElementId> arCellEleId, double dFloorHeight)
		{
			BeamCellData beamCellData = null;
			if (arCellCurve.Count != arCellEleId.Count)
			{
				return beamCellData;
			}
			List<CurveElementData> list = new List<CurveElementData>();
			for (int i = 0; i < arCellCurve.Count; i++)
			{
				Curve curve = arCellCurve[i];
				ElementId elementId = arCellEleId[i];
				if (null != curve && null != elementId)
				{
					Element element = doc.GetElement(elementId);
					BeamOrderType bType = BeamOrderType.BT_NotBeam;
					if (element is Wall)
					{
						bType = BeamOrderType.BT_PrimaryWall;
					}
					else if (element is FamilyInstance)
					{
						bType = BeamOperator.GetBeamOrderType(element as FamilyInstance);
					}
					list.Add(new CurveElementData(curve, elementId, bType));
				}
			}
			CurveArray curveArray = new CurveArray();
			foreach (Curve curve2 in arCellCurve)
			{
				curveArray.Append(curve2);
			}
			XYZ xyz = Geometry.Centroid(curveArray);
			XYZ ptCenter = new XYZ(xyz.X, xyz.Y, dFloorHeight);
			double cellArea = Geometry.CurveAoArea(arCellCurve);
			beamCellData = new BeamCellData();
			beamCellData.m_CellArea = cellArea;
			beamCellData.m_ptCenter = ptCenter;
			beamCellData.m_dFloorHeight = dFloorHeight;
			beamCellData.m_arCurve.AddRange(list);
			return beamCellData;
		}

		private static void CreateLines(List<BeamRegionData> arRegion, Autodesk.Revit.DB.Document doc)
		{
			new List<Curve>();
			foreach (BeamRegionData beamRegionData in arRegion)
			{
				foreach (BeamCellData beamCellData in beamRegionData.m_arBeamCell)
				{
					XYZ ptCenter = beamCellData.m_ptCenter;
					double num = Common.MMtoIntch(300.0);
					XYZ xyz = ptCenter - XYZ.BasisX.Multiply(num);
					XYZ xyz2 = ptCenter + XYZ.BasisX.Multiply(num);
					XYZ xyz3 = ptCenter + XYZ.BasisY.Multiply(num);
					XYZ xyz4 = ptCenter - XYZ.BasisY.Multiply(num);
					Line line = Line.CreateBound(xyz, xyz2);
					Line line2 = Line.CreateBound(xyz3, xyz4);
					doc.Create.NewDetailCurve(doc.ActiveView, line);
					doc.Create.NewDetailCurve(doc.ActiveView, line2);
				}
			}
		}

		private static void TestBeamCellDetail(List<BeamRegionData> arRegion, Autodesk.Revit.DB.Document doc, double dElevation)
		{
			Autodesk.Revit.DB.View viewPlanByElevation = ElementSelector.GetViewPlanByElevation(doc, dElevation);
			if (viewPlanByElevation == null)
			{
				return;
			}
			Level genLevel = viewPlanByElevation.GenLevel;
			new List<Curve>();
			foreach (BeamRegionData beamRegionData in arRegion)
			{
				foreach (BeamCellData beamCellData in beamRegionData.m_arBeamCell)
				{
					List<ElementId> beamIdsByType = beamCellData.GetBeamIdsByType(BeamOrderType.BT_PrimaryBeam);
					beamCellData.GetBeamIdsByType(BeamOrderType.BT_SecondaryBeam);
					List<ElementId> list = new List<ElementId>();
					list.AddRange(beamIdsByType);
					list.AddRange(list);
					List<Element> list2 = new List<Element>();
					foreach (ElementId elementId in list)
					{
						FamilyInstance familyInstance = doc.GetElement(elementId) as FamilyInstance;
						if (familyInstance != null)
						{
							list2.Add(familyInstance);
						}
					}
					WallCreator.CreateCurveWalls(doc, list2, genLevel);
				}
			}
		}

		public static double FloorHeightS = 0.0;

		public static UIDocument uiDocAll;

		public static double m_dZeroTol = 1E-05;

		public static double m_dUpSerch = Common.MMtoIntch(1000.0);
	}
}
